// pages/category/index.js
var app = getApp()
var myhost = app.globalData.myhost
Page({
  /**
   * 页面的初始数据
   */
  data: {
    sysInfo: wx.getSystemInfoSync(),
    categories: [],
    books: [],
    activeKey: 0,
    list: []
  },
  //书籍详情
  detail: function (e) {
    var page = e.currentTarget.dataset.page
    var bookid = e.currentTarget.dataset.bookid
    // console.log(e.currentTarget)
    if (page) {
      wx.navigateTo({
        url: page + '?bookid=' + bookid,
      })
    }
  },
  onChange(event) {
    console.log(event.detail)
    if (event.detail == 0) {
      var templist = []
      wx.request({
        url: myhost + '/book/allBook',
        success: (res) => {
          for (var i = 0; i < res.data.length; i++) {
            wx.request({
              url: myhost + '/book/detail?bookId=' + res.data[i].id,
              success: (res) => {
                res.data.pic = myhost + res.data.pic
                templist.push(res.data)
              }
            })
          }
        }
      })
      setTimeout(() => {
        this.setData({
          list: templist
        })
        // console.log(templist)
      }, 500)
    } else {
      console.log(myhost + '/liu/IdOfStyle?style=' + this.data.categories[event.detail-1].category)
      wx.request({
        url: myhost + '/liu/IdOfStyle?style=' + this.data.categories[event.detail-1].category,
        success: (res) => {
          console.log(res.data)
          for (var i = 0; i < res.data.length; i++) {
            res.data[i].pic = myhost + res.data[i].pic
          }
          this.setData({
            list: res.data
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.request({
      url: myhost + '/liu/GetAllCategory',
      success: (res) => {
        this.setData({
          categories: res.data
        })
      }
    })
    var templist = []
    wx.request({
      url: myhost + '/book/allBook',
      success: (res) => {
        for (var i = 0; i < res.data.length; i++) {
          wx.request({
            url: myhost + '/book/detail?bookId=' + res.data[i].id,
            success: (res) => {
              res.data.pic = myhost + res.data.pic
              templist.push(res.data)
            }
          })
        }
      }
    })
    setTimeout(() => {
      this.setData({
        list: templist
      })
    }, 500)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var templist = []
    wx.request({
      url: myhost + '/book/allBook',
      success: (res) => {
        for (var i = 0; i < res.data.length; i++) {
          wx.request({
            url: myhost + '/book/detail?bookId=' + res.data[i].id,
            success: (res) => {
              res.data.pic = myhost + res.data.pic
              templist.push(res.data)
            }
          })
        }
      }
    })
    setTimeout(() => {
      this.setData({
        list: templist
      })
      // console.log(templist)
    }, 500)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})